//
//  Status.swift
//  kingWeiBo
//
//  Created by King on 15/9/15.
//  Copyright © 2015年 King. All rights reserved.
//

import UIKit
import SDWebImage
class Status: NSObject {
    
    ///  微博创建时间
    var created_at: String?
    ///  微博ID
    var id: Int = 0
    ///  微博信息内容
    var text: String?
    ///  微博来源
    var  source: String?
    /// 行高
    var rowHeight:CGFloat?
    /// 微博配图
    
    var retweeted_status:Status?
    var  pic_urls:[[String:AnyObject]]?{
        didSet{
            if pic_urls?.count == 0{
                return
            }
            savePicStatus = [NSURL]()
            for dict in pic_urls!{
                if let urlString = dict["thumbnail_pic"] as? String {
                    savePicStatus?.append(NSURL(string: urlString)!)
                }
            }
        }
    }
    
    private var savePicStatus:[NSURL]?
    
    /// 微博配图数组 
    var imageURL:[NSURL]?{
    return  retweeted_status == nil ? savePicStatus : retweeted_status?.savePicStatus
    
    }
    /// 用户
    var user: Users?
    
    
    /// 加载微博数据
    class func loadStatus(finished:(dataList:[Status]?, error:NSError?)->()){
        
       
        NetWorkTool.shareTools.loadWeiBoData { (result, error) -> () in
            
            if error != nil{
                
                finished(dataList: nil, error: error)
                return
            }
            
            if let array = (result?["statuses"] as? [[String: AnyObject]]){
                
                var list = [Status]()
                
                
                for dict in array{
                    
                    list.append(Status(dict: dict))
                }
                
//                cacheImage(list, finished: finished)
                finished(dataList: list, error: nil)
                

            }else{
                    finished(dataList: nil, error: nil)
            }
            
        }
    }
    
    
    
    
//    //MARK: - 缓存微博的网络图片
//    private class  func cacheImage(list:[Status],finished:(dataList:[Status]?, error:NSError?)->()){
//        
//        let group = dispatch_group_create()
//        for statues in list{
//            
//            print(statues.savePicStatus)
//            guard let urlImage = statues.savePicStatus else{
//                continue
//            }
//            for url in urlImage{
//                dispatch_group_enter(group)
//                SDWebImageManager.sharedManager().downloadImageWithURL(url, options: SDWebImageOptions(rawValue: 0), progress: nil, completed: { (image, _, _, _, _) -> Void in
//
//                    dispatch_group_leave(group)
//                    
//                })
//                
//            }
//            
////        }
//        dispatch_group_notify(group, dispatch_get_main_queue()) { () -> Void in
//            finished(dataList: list, error: nil)
//            print("over")
//        }
//        
//    }
    //MARK: - KVC赋值
    init(dict:[String: AnyObject]) {
        super.init()
        
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forKey key: String) {
        if key == "user"{
        
            if let dict = value as? [String: AnyObject]{
            
                user = Users(dict: dict)
            }
            return
        }
        
        if key == "retweeted_status"{
            if let dict = value as? [String: AnyObject]{
                retweeted_status = Status(dict: dict)
            }
                return
        }
        super.setValue(value, forKey: key)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
}
